package com.lemon.smartsafetynew.ui.fragment.contact;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.RefreshType;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.ui.fragment.base.BaseSearchPullToRefreshFragment;
import com.lemon.smartsafetynew.entity.event.common.ViewEvent;
import com.lemon.smartsafetynew.entity.event.contact.CompanyEvent;
import com.lemon.smartsafetynew.entity.me.MeCompanyVo;
import com.lemon.smartsafetynew.net.services.RecordServices;
import com.lemon.smartsafetynew.ui.adapter.me.MeCompanyListAdapter;
import com.lemon.smartsafetynew.util.constants.KeyConstants;

import java.util.List;

import kale.adapter.LemonRcvAdapter;

/**
 * Created by Andrew on 2017/10/31.
 */

public class CompanyListFragment extends BaseSearchPullToRefreshFragment<MeCompanyVo, List<MeCompanyVo>>
        implements RecyclerTClickListener<MeCompanyVo>, View.OnClickListener {

    private String search_params = "";

    private static String tag = "";

    @Override
    protected void getBaseArguments() {
        super.getBaseArguments();

        Bundle bundle = getArguments();

        if (bundle != null) {
            if (bundle.containsKey(KeyConstants.KEY_TAG)) {
                tag = bundle.getString(KeyConstants.KEY_TAG);
            }
        }
    }

    public static CompanyListFragment newInstance(String tag) {

        Bundle args = new Bundle();
        args.putString(KeyConstants.KEY_TAG, tag);

        CompanyListFragment fragment = new CompanyListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onPullRefresh() {
        LemonRequestParams params = new LemonRequestParams("");
        params.put("company_code", "");
        params.put("company_name", search_params);
        params.put("status", "");
        params.put("pageNum", currentPage);
        params.put("pageSize", pageSize);
        RecordServices
                .init()
                .getCompanyList(RefreshType.REFRESH, params);
    }

    @Override
    public void onLoadMore() {

        LemonRequestParams params = new LemonRequestParams("");
        params.put("company_code", "");
        params.put("company_name", search_params);
        params.put("status", "");
        params.put("pageNum", currentPage);
        params.put("pageSize", pageSize);
        RecordServices
                .init()
                .getCompanyList(RefreshType.LOAD_MORE, params);
    }

    @Override
    public boolean goRefreshOnCreate() {
        return true;
    }

    @Override
    public LemonRcvAdapter<MeCompanyVo> getLemonAdapter() {
        return new MeCompanyListAdapter(mDataList, this,false);
    }

    @Override
    public String getHttpTag() {
        return KeyConstants.URL_SEARCH_COMPANY_BY_NAME;
    }

    @Override
    public void onItemClick(MeCompanyVo data, int position) {

        CompanyEvent companyEvent = new CompanyEvent();
        companyEvent.setCompanyVo(data);
        companyEvent.setTag(tag);
        AppService.getBus().post(new ViewEvent(CompanyListFragment.class.getName()));
        AppService.getBus().post(companyEvent);

    }

    @Override
    protected void initViews() {
        super.initViews();

        txtClearn.setOnClickListener(this);
        lineRight.setOnClickListener(this);
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                if (actionId == KeyEvent.ACTION_DOWN || actionId == EditorInfo.IME_ACTION_DONE) {
                    //业务代码
                    search_params = editText.getText().toString().trim();
                    query(false);
                }
                return true;
            }
        });

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                search_params = s.toString().trim();
                query(true);

                if (!"".equals(s.toString())) {
                    txtClearn.setVisibility(View.VISIBLE);
                } else {
                    txtClearn.setVisibility(View.GONE);
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.lineRight:
                //点击搜索按钮
                search_params = editText.getText().toString().trim();
                query(false);

                break;

            case R.id.txtClearn:
                editText.getText().clear();
                break;

            default:
                break;
        }
    }

    private void query(boolean realTime) {

        // 企业搜索
        if (!realTime) {
            // 获取刷新
            doPullRefresh();
        } else {
            if ("".equals(search_params)) {
                // 当搜索内容为空时，显示搜索一次全部列表
                doPullRefresh();
            }
        }

    }

}

